package com.sc.nft.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.DigitalCollectionProductInfoSubscribe;
import com.sc.nft.entity.EcologyHatchCollecionShareProfitConf;
import com.sc.nft.entity.EcologyHatchShareProfitMethodConf;
import com.sc.nft.enums.EcologyHatchShareProfitMethodConfTypeEnum;
import com.sc.nft.mapper.EcologyHatchShareProfitMethodConfMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.Objects;

/**
 * @Author：LiGen
 * @Package：com.sc.nft.dao
 * @Project：sc_metago
 * @name：EcologyHatchShareProfitMethodConfDao
 * @Date：2024/7/11 14:47
 * @Filename：EcologyHatchShareProfitMethodConfDao
 */
@Slf4j
@Repository
public class EcologyHatchShareProfitMethodConfDao extends ServiceImpl<EcologyHatchShareProfitMethodConfMapper, EcologyHatchShareProfitMethodConf> {

    /**
     * 获取生态熔炼分润方式
     *
     * @param ecologyHatchId
     * @return
     */
    public EcologyHatchShareProfitMethodConf getOneByEcologyHatchId(Long ecologyHatchId) {
        LambdaQueryWrapper<EcologyHatchShareProfitMethodConf> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EcologyHatchShareProfitMethodConf::getEcologyHatchId, ecologyHatchId);
        wrapper.orderByDesc(EcologyHatchShareProfitMethodConf::getId);
        wrapper.last("LIMIT 1");
        return getOne(wrapper);
    }


    /**
     * 获取生态熔炼分润方式
     *
     * @param resonanceServiceCenterDaoId
     * @return
     */
    public EcologyHatchShareProfitMethodConf getOneByResonanceServiceCenterDaoId(Long resonanceServiceCenterDaoId,Long id) {
        LambdaQueryWrapper<EcologyHatchShareProfitMethodConf> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EcologyHatchShareProfitMethodConf::getResonanceServiceCenterDaoId, resonanceServiceCenterDaoId);
        if (Objects.nonNull(id)){
            wrapper.ne(EcologyHatchShareProfitMethodConf::getId,id);
        }
        wrapper.orderByDesc(EcologyHatchShareProfitMethodConf::getId);
        wrapper.last("LIMIT 1");
        return getOne(wrapper);
    }

    /**
     * 修改生态分润方法配置
     *
     * @param ecologyHatchId  熔炼配置id
     * @param type            分润类型
     * @param resonanceStatus 是否开启共振
     */
    public void updateByEcologyHatchId(Long ecologyHatchId, EcologyHatchShareProfitMethodConfTypeEnum type, Boolean resonanceStatus, BigDecimal resonanceServiceCenterCoefficient, Long resonanceServiceCenterDaoId, Long resonanceDaoId, EcologyHatchShareProfitMethodConf ecologyHatchShareProfitMethodConfInDb) {
        LambdaQueryWrapper<EcologyHatchShareProfitMethodConf> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(EcologyHatchShareProfitMethodConf::getEcologyHatchId, ecologyHatchId);
        EcologyHatchShareProfitMethodConf ecologyHatchShareProfitMethodConf = new EcologyHatchShareProfitMethodConf();
        ecologyHatchShareProfitMethodConf.setType(type);
        ecologyHatchShareProfitMethodConf.setResonanceStatus(resonanceStatus);
        if (Objects.nonNull(resonanceServiceCenterDaoId) && (Objects.isNull(ecologyHatchShareProfitMethodConfInDb.getResonanceServiceCenterDaoId()) || !ecologyHatchShareProfitMethodConfInDb.getResonanceServiceCenterDaoId().equals(resonanceServiceCenterDaoId))) {
            ecologyHatchShareProfitMethodConf.setResonanceServiceCenterDaoId(resonanceServiceCenterDaoId);
        }
        if (Objects.nonNull(resonanceDaoId) && (Objects.isNull(ecologyHatchShareProfitMethodConfInDb.getResonanceDaoId()) || !ecologyHatchShareProfitMethodConfInDb.getResonanceDaoId().equals(resonanceDaoId))) {
            ecologyHatchShareProfitMethodConf.setResonanceDaoId(resonanceDaoId);
        }
        ecologyHatchShareProfitMethodConf.setResonanceServiceCenterCoefficient(resonanceServiceCenterCoefficient);
        baseMapper.update(ecologyHatchShareProfitMethodConf, wrapper);
    }

    public void updateResonanceServiceCenterCoefficient(Long id,BigDecimal resonanceServiceCenterCoefficient) {
        LambdaQueryWrapper<EcologyHatchShareProfitMethodConf> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(EcologyHatchShareProfitMethodConf::getId, id);
        EcologyHatchShareProfitMethodConf ecologyHatchShareProfitMethodConf = new EcologyHatchShareProfitMethodConf();
        ecologyHatchShareProfitMethodConf.setResonanceServiceCenterCoefficient(resonanceServiceCenterCoefficient);
        baseMapper.update(ecologyHatchShareProfitMethodConf, wrapper);
    }

    /**
     * 修改生态分润方法是否开启共振
     *
     * @param ecologyHatchId  熔炼配置id
     * @param resonanceStatus 是否开启共振
     */
    public void updateStatusByEcologyHatchId(Long ecologyHatchId, Boolean resonanceStatus, Long resonanceServiceCenterDaoId,Long resonanceServiceCenterDaoIdInDb) {
        LambdaQueryWrapper<EcologyHatchShareProfitMethodConf> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(EcologyHatchShareProfitMethodConf::getEcologyHatchId, ecologyHatchId);
        EcologyHatchShareProfitMethodConf ecologyHatchShareProfitMethodConf = new EcologyHatchShareProfitMethodConf();
        ecologyHatchShareProfitMethodConf.setResonanceStatus(resonanceStatus);
        if (Objects.isNull(resonanceServiceCenterDaoIdInDb)){
            ecologyHatchShareProfitMethodConf.setResonanceServiceCenterDaoId(resonanceServiceCenterDaoId);
        }
        baseMapper.update(ecologyHatchShareProfitMethodConf, wrapper);
    }

    /**
     * 修改生态分润方法共振所属品牌id
     *
     * @param ecologyHatchId 熔炼配置id
     * @param resonanceDaoId 共振所属品牌id
     */
    public void updateResonanceDaoIdByEcologyHatchId(Long ecologyHatchId, Long resonanceDaoId) {
        LambdaQueryWrapper<EcologyHatchShareProfitMethodConf> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(EcologyHatchShareProfitMethodConf::getEcologyHatchId, ecologyHatchId);
        EcologyHatchShareProfitMethodConf ecologyHatchShareProfitMethodConf = new EcologyHatchShareProfitMethodConf();
        ecologyHatchShareProfitMethodConf.setResonanceDaoId(resonanceDaoId);
        baseMapper.update(ecologyHatchShareProfitMethodConf, wrapper);
    }

    /**
     * 修改生态分润方法共振所属品牌id
     *
     * @param id                                id
     * @param resonanceServiceCenterCoefficient 共振服务中心系数
     */
    public void updateResonanceServiceCenterCoefficientById(Long id, BigDecimal resonanceServiceCenterCoefficient) {
        LambdaQueryWrapper<EcologyHatchShareProfitMethodConf> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(EcologyHatchShareProfitMethodConf::getId, id);
        EcologyHatchShareProfitMethodConf ecologyHatchShareProfitMethodConf = new EcologyHatchShareProfitMethodConf();
        ecologyHatchShareProfitMethodConf.setResonanceServiceCenterCoefficient(resonanceServiceCenterCoefficient);
        baseMapper.update(ecologyHatchShareProfitMethodConf, wrapper);
    }

}
